package com.lucy.common.config;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Validator;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Optional;

/**
 * @author kindear
 * 获取当前操作用户信息
 */
@Configuration
public class JpaAuditorConfig implements AuditorAware<Long> {
    @Override
    public Optional<Long> getCurrentAuditor() {
        Object loginId = StpUtil.getLoginIdDefaultNull();
        if (Validator.isEmpty(loginId)){
            return Optional.of(defaultAuditor());
        }else {
            return Optional.of(Long.parseLong(loginId.toString()));
        }
    }
    public Long defaultAuditor(){
        return 0L;
    }
}
